package com.wu.controller;/**
 * @author sun
 * @date 2022/3/21 - 7:45
 */

import com.wu.pojo.Hotsearch;
import com.wu.pojo.Images;
import com.wu.pojo.vo.HotsearchVo;
import com.wu.service.HotsearchService;
import com.wu.service.ImagesService;
import com.wu.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: TODO
 * @author 孙
 * @date 2022/3/21 7:45
 * @version 1.0
 */
@RestController
@Api(tags="Hotsearch模块")
@CrossOrigin
@RequestMapping("hotsearch")
public class HotsearchController {
    @Autowired
    private HotsearchService hotsearchService;
    @Autowired
    private ImagesService imagesService;
    @PostMapping("/insertHotsearch")
    @ApiOperation(value="增加Hotsearch")
    public R insertHotsearch(@RequestBody Hotsearch hotsearch){
        try {
            hotsearchService.insertSelective(hotsearch);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }
    @PostMapping("/insertHotsearchVo")
    @ApiOperation(value="增加HotsearchVo")
    public R insertHotsearch(@RequestBody HotsearchVo hotsearchVo){
        try {
            hotsearchService.insertSelective(hotsearchVo);
             List<Images> images = hotsearchVo.getImages();
            if (images!=null && images.size()>0){
                for (Images image : images) {
                    imagesService.insertSelective(image);
                }
            }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }


    @GetMapping("/deleteHotsearch/{id}")
    @ApiOperation(value="删除Hotsearch")
    public R deleteHotsearch(Integer id){
        try {
            hotsearchService.deleteByPrimaryKey(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @PutMapping("/updateHotsearch")
    @ApiOperation(value="更新Hotsearch")
    public R updateHotsearch(@RequestBody Hotsearch hotsearch){
        try {
            hotsearchService.updateByPrimaryKeySelective(hotsearch);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/findHotsearch")
    @ApiOperation(value="查询HotsearchList")
    public R findHotsearch(Hotsearch hotsearch){
        try {
            List<HotsearchVo> list= hotsearchService.findHotsearch(hotsearch);
            return R.ok().data("hotsearchList",list);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/findHotsearchById/{id}")
    @ApiOperation(value="通过id查询Hotsearch")
    public R findHotsearch(@PathVariable("id") Integer id){
        try {
            Hotsearch hotsearch= hotsearchService.selectByPrimaryKey(id);
            return R.ok().data("hotsearch",hotsearch);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
}
